package fms.admin;
import fms.classes.*;
import fms.models.TestDAO;
import fms.models.UserDAO;

import java.awt.List;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.IFormValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;


public class AddUser extends WebPage{  
	
					
	private Model idNoModel1 = new Model("");
	private Model idNoModel2 = new Model("");
	private Model fullNameModel = new Model("");
	private Model yearLevelModel = new Model("");
	private Model userOrgModel = new Model("");
	private Model userPositionModel = new Model("");
	private Model fromSYModel = new Model("");
	private Model toSYModel = new Model("");
	private Model semesterModel = new Model("");
	private Model userIdModel = new Model("");
	private Model userPasswordModel = new Model(""); 

	
	public AddUser(){ 
		
				
		Form addUserForm = new Form("addUserForm");
		
		TextField idNoField1 = new TextField("idNo1", idNoModel1);
		idNoField1.setRequired(true);
		addUserForm.add(idNoField1);
		
		TextField idNoField2 = new TextField("idNo2", idNoModel2);
		idNoField2.setRequired(true);
		addUserForm.add(idNoField2);
		
	
		TextField fullNameField = new TextField("full_name", fullNameModel);
		fullNameField.setRequired(true);
		addUserForm.add(fullNameField);
		
	
		DropDownChoice yearLevelField, userOrgField, userPositionField, semesterField;
		
		ArrayList yearLevelChoice = new ArrayList();
		
					yearLevelChoice.add("1");
					yearLevelChoice.add("2");
					yearLevelChoice.add("3");
					yearLevelChoice.add("4");
					yearLevelChoice.add("5");
		
					yearLevelField = new DropDownChoice("year_level", yearLevelModel, yearLevelChoice);
					
					addUserForm.add(yearLevelField);
										
		
		ArrayList userOrgChoice = new ArrayList();
		
					userOrgChoice.add("org1");
					userOrgChoice.add("org2");
					userOrgChoice.add("org3");
					userOrgChoice.add("org4");
					userOrgChoice.add("org5");
					userOrgChoice.add("org6");
					userOrgChoice.add("org7");
					userOrgChoice.add("org8");
					userOrgChoice.add("org9");
					userOrgChoice.add("org10");
					
					userOrgField = new DropDownChoice("user_org", userOrgModel, userOrgChoice);
					
					addUserForm.add(userOrgField);
		
		ArrayList userPositionChoice = new ArrayList();
		
					userPositionChoice.add("treasurer");
					userPositionChoice.add("bookeeper");
					userPositionChoice.add("auditior");
					userPositionChoice.add("buficom chair");
					userPositionChoice.add("adviser");
					userPositionChoice.add("petty cash custodian");
					
					userPositionField = new DropDownChoice("user_position", userPositionModel, userPositionChoice);
					
					addUserForm.add(userPositionField);
					
					
		
		ArrayList semesterChoice = new ArrayList();
					
					semesterChoice.add("1");
					semesterChoice.add("2");
					
					semesterField = new DropDownChoice("semester", semesterModel, semesterChoice);
					
					addUserForm.add(semesterField);
	
		TextField fromSYField = new TextField("from_SY", fromSYModel);
		fromSYField.setRequired(true);
		addUserForm.add(fromSYField);
		
		TextField toSYField = new TextField("to_SY", toSYModel);
		toSYField.setRequired(true);
		addUserForm.add(toSYField);
	
		TextField userIdField =  new TextField("user_id", userIdModel);
		userIdField.setRequired(true);
		addUserForm.add(userIdField);
		
		PasswordTextField userPasswordField = new PasswordTextField("user_password", userPasswordModel);
		userPasswordField.setRequired(true);
		addUserForm.add(userPasswordField);  
		
	
		
		add(addUserForm);
		
		Button addUserButton1 = new Button("addUserButton"){
					
			public void onSubmit(){
				
				String idNo1 = (String) idNoModel1.getObject();
				String idNo2 = (String) idNoModel2.getObject();
				String fullName = (String) fullNameModel.getObject();
				String the_yearLevel = (String) yearLevelModel.getObject();
				String userOrg = (String) userOrgModel.getObject();
				String userPosition = (String) userPositionModel.getObject();
				String the_fromSY = (String) fromSYModel.getObject();
				String the_toSY = (String) toSYModel.getObject();
				String the_semester = (String) semesterModel.getObject();
				String userId = (String) userIdModel.getObject();
				String userPassword = (String) userPasswordModel.getObject();  
				
				String the_idNo = idNo1 + idNo2;
				
				int idNo = Integer.parseInt(the_idNo);
	
				int yearLevel = Integer.parseInt(the_yearLevel);
				int fromSY = Integer.parseInt(the_fromSY);
				int toSY = Integer.parseInt(the_toSY);
				int semester = Integer.parseInt(the_semester);
				
				User user = new User(idNo, fullName,yearLevel, userOrg, userPosition, fromSY, toSY, semester, userId, userPassword);
				
				//UserDAO getDBquerry = new UserDAO();
				UserDAO getDBquerry1 = new UserDAO();
				
				
				getDBquerry1.insertUser(user);
				
				System.out.println("user: " + user +
								    "id_no" + user.getIdNo() +
								    "name: " + user.getName());
				
				
				
				}
		
		
	};
	
	addUserForm.add(addUserButton1);
	
		}
}
